﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using System.Data.Sql;
using System.Data.SqlTypes;
using DatabaseModule;

namespace EquationsBankReader.EquationsRead
{
    class Xml_Read
    {
        #region Class variables
        private EquationsDBEntities db = new EquationsDBEntities();
        private static  UInt32                    GlobalEquationNumber = 0;
        #endregion

        #region Constructor

        public Xml_Read()
        {
            //Constructor
        }

        #endregion
        // Adding the equations to the Database


        public void AddListToDataBase(List<XML_Parser> Equations)
        {
            foreach (XML_Parser ParsedFile in Equations)
            {
                for (UInt32 Loop_Equation_Number = 1; Loop_Equation_Number <= UInt32.MaxValue; Loop_Equation_Number++)
                {
                    ParsedFile.Equation_Number = Loop_Equation_Number;
                    if (ParsedFile.Equation != null)
                    {
                      GlobalEquationNumber++;
                      // Adding new equation
                      Equation eqn = new Equation()
                      {
                          Name  = "Equation",
                          Value = GlobalEquationNumber.ToString()
                      };
                      //Adding the entry in the Settings table
                      if (ParsedFile.Settings != null)
                      {
                        Setting Set = new Setting()
                        {
                          Name = ParsedFile.Settings_Value,
                          Equation = eqn
                        };
                        // Adding the entry in the EnableSolver table
                        EnableSolver EnSol = new EnableSolver()
                        {
                          Name = ParsedFile.V_Equation_00600.Name.ToString(),
                          Value = ParsedFile.V_Equation_00600_Value,
                          Setting = Set
                        };
                        db.AddToSettings(Set);
                      }
                        // Reading Equation Path
                        for (UInt32 Loop_Path_Number = 1; Loop_Path_Number <= UInt32.MaxValue; Loop_Path_Number++)
                        {
                            ParsedFile.Path_Number = Loop_Path_Number;
                            if (ParsedFile.V_Equation_00001 != null)
                            {
                                Equation_Path path = new Equation_Path()
                                {
                                    Name = ParsedFile.V_Equation_00001.Name.ToString(),
                                    Value = ParsedFile.V_Equation_00001_Value,
                                    Equation = eqn
                                };

                                foreach (var lang in ParsedFile.GetPathLanguages(ParsedFile.V_Equation_00001))
                                {
                                    Equation_Path_Language eqnPathLang = new Equation_Path_Language()
                                    {
                                        Name = lang[0],
                                        Value = lang[1],
                                        Equation_Path = path
                                    };

                                    db.AddToEquation_Path_Language(eqnPathLang);
                                    path.Equation_Path_Language.Add(eqnPathLang);
                                }

                                db.AddToEquation_Path(path);
                                eqn.Equation_Path.Add(path);
                            }
                            else
                            {
                                break;
                            }
                        }


                        // Reading equation name
                        for (UInt32 Loop_Name_Number = 1; Loop_Name_Number <= UInt32.MaxValue; Loop_Name_Number++)
                        {
                            ParsedFile.Name_Number = Loop_Name_Number;
                            if (ParsedFile.V_Equation_00100 != null)
                            {
                                // Read equation name V_Equation_00101_Value & V_Equation_00102_Value
                                Equation_Name name = new Equation_Name()
                                {
                                    Name     = ParsedFile.V_Equation_00100.Name.ToString(),
                                    Value    = ParsedFile.V_Equation_00100_Value,
                                    Equation = eqn
                                };

                                foreach (var lang in ParsedFile.GetNameLanguages(ParsedFile.V_Equation_00100))
                                {
                                    Equation_Name_Language eqnNameLang = new Equation_Name_Language()
                                    {
                                        Name          = lang[0],
                                        Value         = lang[1],
                                        Equation_Name = name
                                    };

                                    db.AddToEquation_Name_Language(eqnNameLang);
                                    name.Equation_Name_Language.Add(eqnNameLang);
                                }

                                eqn.Equation_Name.Add(name);
                            }
                            else
                            {
                                break;
                            }
                        }


                        // Reading Formula
                        for (UInt32 Loop_Formula_Number = 1; Loop_Formula_Number <= UInt32.MaxValue; Loop_Formula_Number++)
                        {
                            ParsedFile.Formula_Number = Loop_Formula_Number;
                            if (ParsedFile.V_Equation_00200 != null)
                            {
                                // Reading the formula value V_Equation_00201_Value
                                Equation_Formula formula = new Equation_Formula() 
                                {
                                    Name           = ParsedFile.V_Equation_00200.Name.ToString(),
                                    Value          = ParsedFile.V_Equation_00200_Value,
                                    Representation = ParsedFile.V_Equation_00201_Value,
                                    Equation       = eqn
                                    //Image (* To be done after rendering *)
                                };


                                // Reading the variables of the formula
                                for (UInt32 Loop_Formula_Variable_Number = 1; Loop_Formula_Variable_Number <= UInt32.MaxValue; Loop_Formula_Variable_Number++)
                                {
                                    ParsedFile.Formula_Variable_Number = Loop_Formula_Variable_Number;
                                    if (ParsedFile.V_Equation_00202 != null)
                                    {
                                        // Read variable type V_Equation_00203_Value
                                        Equation_Formula_Variable variable = new Equation_Formula_Variable()
                                        {
                                            Name             = ParsedFile.V_Equation_00202.Name.ToString(),
                                            TypeName         = ParsedFile.V_Equation_00203.Name.ToString(),
                                            TypeValue        = ParsedFile.V_Equation_00203_Value,
                                            Value            = ParsedFile.V_Equation_00202_Value,
                                            Equation_Formula = formula
                                        };

                                    }
                                    else
                                    {
                                        break;
                                    }
                                }
                            }
                            else
                            {
                                break;
                            }
                        }


                        //Reading references
                        for (UInt32 Loop_Reference_Number = 1; Loop_Reference_Number <= UInt32.MaxValue; Loop_Reference_Number++)
                        {
                            ParsedFile.Reference_Number = Loop_Reference_Number;
                            if (ParsedFile.V_Equation_00300 != null)
                            {
                                // Reading reference value V_Equation_00301_Value
                                Equation_Reference reference = new Equation_Reference() 
                                {
                                    Name           = ParsedFile.V_Equation_00300.Name.ToString(),
                                    ReferenceName  = ParsedFile.V_Equation_00301.Name.ToString(),
                                    ReferenceValue = ParsedFile.V_Equation_00301_Value,
                                    Value          = ParsedFile.V_Equation_00300_Value,
                                    Equation       = eqn
                                };
                            }
                            else
                            {
                                break;
                            }
                        }


                        // Reading cite as
                        for (UInt32 Loop_Citation_Number = 1; Loop_Citation_Number <= UInt32.MaxValue; Loop_Citation_Number++)
                        {
                            ParsedFile.Citation_Number = Loop_Citation_Number;
                            if (ParsedFile.V_Equation_00400 != null)
                            {
                                // Reading citation value V_Equation_00401_Value
                                Equation_Citation citation = new Equation_Citation()
                                {
                                    Name          = ParsedFile.V_Equation_00400.Name.ToString(),
                                    Value         = ParsedFile.V_Equation_00400_Value,
                                    CitationName  = ParsedFile.V_Equation_00401.Name.ToString(),
                                    CitationValue = ParsedFile.V_Equation_00401_Value,
                                    Equation      = eqn
                                };
                            }
                            else
                            {
                                break;
                            }
                        }


                        // Reading Equation description
                        for (UInt32 Loop_Equation_Description_Number = 1; Loop_Equation_Description_Number <= UInt32.MaxValue; Loop_Equation_Description_Number++)
                        {
                            ParsedFile.Equation_Description_Number = Loop_Equation_Description_Number;
                            if (ParsedFile.V_Equation_00500 != null)
                            {
                                // Reading the equation description V_Equation_00501_Value & V_Equation_00502_Value
                                Equation_Description description = new Equation_Description()
                                {
                                    Name     = ParsedFile.V_Equation_00500.Name.ToString(),
                                    Value    = ParsedFile.V_Equation_00500_Value,
                                    Equation = eqn
                                };

                                foreach (var lang in ParsedFile.GetDescriptionLanguages(ParsedFile.V_Equation_00500))
                                {
                                    Equation_Description_Language eqnDescriptionLang = new Equation_Description_Language()
                                    {
                                        Name = lang[0],
                                        Value = lang[1],
                                        Equation_Description = description
                                    };

                                    db.AddToEquation_Description_Language(eqnDescriptionLang);
                                    description.Equation_Description_Language.Add(eqnDescriptionLang);
                                }
                                eqn.Equation_Description.Add(description);
                            }
                            else
                            {
                                break;
                            }
                        }
                        db.AddToEquations(eqn);
                    }
                    else
                    {
                        break;
                    }
                }
            }

            db.SaveChanges();
        }
    }
}
